# 计算24的程序
from itertools import permutations
from datetime import datetime
def cal24(a,b,c,d,output=0):
    print(a,b,c,d)
    def add(x,y):
        return x+y
    def minus(x,y):
        return x-y
    def mul(x,y):
        return x*y
    def div(x,y):
        return x/y
    add.__str__="+"
    minus.__str__="-"
    mul.__str__="x"
    div.__str__="÷"
    ss=[add,minus,mul,div]
    datas=[]
#     控制输出地方，0为终端输出，1为文件输出，2为同时输出
    with open('result.txt','at') as f:
        f.write('本次计算的是:{} {} {} {}\n计算时间{}\n==================== \n'.format(a,b,c,d,datetime.now().strftime("%Y年%m月%d日%H时%M分%S秒")))
        for data in permutations([a,b,c,d]):
            if data in datas:
                continue
            else:
                datas.append(data)
                for i in ss:
                    for j in ss:
                        for k in ss:
                            try:
                                if i(j(k(data[0],data[1]),data[2]),data[3]) == 24:
                                    if output == 0 or output == 2:
                                        print('((',data[0],k.__str__,data[1],')',j.__str__,data[2],')',i.__str__,data[3])
                                    if output == 1 or output == 2:
                                        f.write('(( {} {} {} ) {} {} ) {} {}\n'.format(data[0],k.__str__,data[1],j.__str__,data[2],i.__str__,data[3]))


                                if i(j(data[0],data[1]),k(data[2],data[3])) == 24:
                                    if output == 0 or output == 2:
                                        print('(',data[0],j.__str__,data[1],')',i.__str__,'(',data[2],k.__str__,data[3],')')
                                    if output == 1 or output == 2:
                                        f.write('(( {} {} {} ) {} ( {} {} {} )\n'.format(data[0],j.__str__,data[1],i.__str__,data[2],k.__str__,data[3]))

                                        
                                if i(j(data[0],k(data[1],data[2])),data[3]) == 24:
                                    if output == 0 or output == 2:
                                        print('(',data[0],j.__str__,'(',data[1],k.__str__,data[2],'))',i.__str__,data[3])
                                    if output == 1 or output == 2:
                                        f.write('( {} {} ( {} {} {} )) {} {}\n'.format(data[0],j.__str__,data[1],k.__str__,data[2],i.__str__,data[3]))

                                    
                                if i(data[0],j(k(data[1],data[2]),data[3])) == 24:
                                    if output == 0 or output == 2:
                                        print(data[0],i.__str__,'((',data[1],k.__str__,data[2],')',j.__str__,data[3],')')
                                    if output == 1 or output == 2:
                                        f.write('{} {} (( {} {} {} ) {} {} )\n'.format(data[0],i.__str__,data[1],k.__str__,data[2],j.__str__,data[3]))

                                    
                                if i(data[0],j(data[1],k(data[2],data[3]))) == 24:
                                    if output == 0 or output == 2:
                                        print(data[0],i.__str__,'(',data[1],j.__str__,'(',data[2],k.__str__,data[3],'))')
                                    if output == 1 or output == 2:
                                        f.write('{} {} ( {} {} ( {} {} {} ))\n'.format(data[0],i.__str__,data[1],j.__str__,data[2],k.__str__,data[3]))
    
                            except:
                                continue
                            
    print('计算完毕')
    f.flush
    return

import sys
cal24(int(sys.argv[1]),int(sys.argv[2]),int(sys.argv[3]),int(sys.argv[4]),output=0)
# print(sys.argv[1])
